package com.atguigu.gmall.common.config.minio;


import com.atguigu.gmall.common.config.minio.service.Impl.FileUploadServiceImpl;
import com.atguigu.gmall.product.service.FileUploadService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: MinioAutoConfiguration
 * Package: com.atguigu.gmall.product.config
 * Description:
 *
 * @Author ltystart
 * @Create 2025/8/21 9:58
 * @Version 1.0
 */
//1、MinioProperties 所有属性和文件进行绑定
//2、MinioProperties 放到容器中
@EnableConfigurationProperties(MinioProperties.class)
@Configuration
public class MinioAutoConfiguration {
    @Autowired
    private MinioProperties minioProperties;
    @Bean
    public MinioClient minioClient() throws Exception{
        //1.创建MinioClient对象
        MinioClient minioClient = MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
        //2.判断桶是否存在
        boolean isExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
        if (!isExists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
        }
        return minioClient;
    }

    @Bean
    public FileUploadService fileUploadService(){
        FileUploadServiceImpl service = new FileUploadServiceImpl();
        return service;
    }
}
